package cn.itcast.service.impl;

import cn.itcast.domain.Admin;
import cn.itcast.domain.PageBean;
import cn.itcast.mapper.AdminMapper;
import cn.itcast.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 查询管理员
     * @return 返回集合
     */
    @Override
    public PageBean findAdmin(String currentPage, String rows) {
        PageBean pageBean = new PageBean();
        int currentPag = Integer.parseInt(currentPage);
        int pageSize = Integer.parseInt(rows);
        List<Admin> admin = adminMapper.findAdmin();
        int size = admin.size();
        pageBean.setTotalCount(size);

        int totalPage=size % pageSize ==0 ? size/pageSize : (size/pageSize)+1;
        pageBean.setTotalPage(totalPage);

        int start = (currentPag-1)*pageSize;

        List<Admin> admin1 = adminMapper.findAdminOne(start, pageSize);
        pageBean.setList(admin1);

        pageBean.setCurrentPage(currentPag);
        pageBean.setPageSize(pageSize);
        return pageBean;
    }

    @Override
    public List<Admin> findAdmin() {
        return adminMapper.findAdmin();
    }

    /**
     * 添加管理员
     * @param admin
     */
    @Override
    public void adminSave(Admin admin) {
        adminMapper.adminSave(admin);
    }
    /**
     * 管理员登陆成功
     */
    @Override
    public Admin adminLogin(String username,String  password) {
        Admin admin1 = adminMapper.adminLogin(username,password);
        System.out.println(admin1);
        return admin1;
    }

    @Override
    public void del(int aid) {
        adminMapper.del(aid);
    }


}
